﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 组态设计明细表服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class BoardDisplayDesignDetailService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<BoardDisplayDesignDetail> _rep;
    public BoardDisplayDesignDetailService(SqlSugarRepository<BoardDisplayDesignDetail> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询组态设计明细表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<BoardDisplayDesignDetailOutput>> Page(BoardDisplayDesignDetailInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.DesignNo.Contains(input.SearchKey.Trim())
                || u.ItemType.Contains(input.SearchKey.Trim())
                || u.Color.Contains(input.SearchKey.Trim())
            )
            .WhereIF(input.MainID>0, u => u.MainID == input.MainID)
            .WhereIF(!string.IsNullOrWhiteSpace(input.DesignNo), u => u.DesignNo.Contains(input.DesignNo.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.ItemType), u => u.ItemType.Contains(input.ItemType.Trim()))
            .WhereIF(input.Top>0, u => u.Top == input.Top)
            .WhereIF(input.Left>0, u => u.Left == input.Left)
            .WhereIF(input.Width>0, u => u.Width == input.Width)
            .WhereIF(input.Height>0, u => u.Height == input.Height)
            .WhereIF(input.EndTop > 0, u => u.EndTop == input.EndTop)
            .WhereIF(input.EndLeft>0, u => u.EndLeft == input.EndLeft)
            .WhereIF(input.Direction>0, u => u.Direction == input.Direction)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Color), u => u.Color.Contains(input.Color.Trim()))
            .WhereIF(input.Radius>0, u => u.Radius == input.Radius)
            .Select<BoardDisplayDesignDetailOutput>();
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加组态设计明细表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddBoardDisplayDesignDetailInput input)
    {
        var entity = input.Adapt<BoardDisplayDesignDetail>();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除组态设计明细表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteBoardDisplayDesignDetailInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
        //await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新组态设计明细表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateBoardDisplayDesignDetailInput input)
    {
        var entity = input.Adapt<BoardDisplayDesignDetail>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取组态设计明细表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<BoardDisplayDesignDetail> Detail([FromQuery] QueryByIdBoardDisplayDesignDetailInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取组态设计明细表列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<BoardDisplayDesignDetailOutput>> List([FromQuery] BoardDisplayDesignDetailInput input)
    {
        return await _rep.AsQueryable().Select<BoardDisplayDesignDetailOutput>().ToListAsync();
    }


    /// <summary>
    /// 获取组态设计明细表列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "getExtDetail")]
    public async Task<List<BoardDisplayDesignDetailOutput>> getExtDetail(long mainid)
    {
        return await _rep.AsQueryable().Where(u=>u.MainID==mainid).Select<BoardDisplayDesignDetailOutput>().ToListAsync();
    }





}

